package commands.player
import static org.groovymud.utils.MessengerUtils.sendMessageToRoom
import utils.MatchedObject
import utils.GenericSyntaxParser
import org.groovymud.object.Container
import static org.groovymud.utils.WordUtils.*/**
 *  puts one object into another
 *  put [all] objectName[s] [index] (in|into) objectName[s] [index]
 */
def stream = source.getTerminalOutput()

GenericSyntaxParser parser = new GenericSyntaxParser()
parser.parse(argstr, "(in|into)")

if(parser.subjectObject != null){
    // can only put what you are holding into other stuff
    if(parser.prepositionObject != null){
		obj = parser.prepositionObject.findObjectInContainer(source)
		subjObj = parser.subjectObject.findObjectInContainer(source)
		if(subjObj instanceof Container){
		    if(obj instanceof Set){
		        def map = source.getMudObjectsMap().clone()
			    def remove = [] as Set			    
			    actualObjs.each{
				    obj ->
				       if(subjObj.checkCanAddItem(obj)){
				           subjObj.addItem(obj)
					    }else{
							stream.writeln("You cannot put ${affixIndefiniteArticle(obj)} into ${affixDefiniteArticle(subjObj)}.")							
							source.removeMudObject(obj, map);
					    }
				    
				}			 
			    contentsDescription = source.getContentsDescription(map, source, false)
		    }else{
		        contentsDescription = affixIndefinateArticle(obj);
			     
			    if(source.checkCanAddItem(obj)){
					source.addItem obj 
			    }
			    else{
					stream.writeln "You cannot put $contentsDescription into $subjObj"
					return 
			    }
			}
			if(contentsDescription.trim() != ""){
    		    def scopeMsg = "${affixDefiniteArticle(source)} puts ${contentsDescription} into ${affixIndefiniteArticle(subjObj)}."
    		    def sourceMsg = "You put $contentsDescription into ${affixDefiniteArticle(subjObj)}."
    		    sendMessageToRoom(source, sourceMsg, scopeMsg)
			}
		}
	}
}
